package com.hdy.javachatroomdemo.Controller;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Component
//这个类继承实现spring提供的webSocket类(TextWebSocketHandler)传输文本类型的数据
//这只是实现了websocket类,但还没有在spring中注册,需要额外再创建一个类实现注册
public class TestWebSocketController extends TextWebSocketHandler {

    @Override
    //在handler类收到消息时自动调用
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        System.out.println("handler类收到消息:"+message.toString());
        //session里面记录着会话双方的信息
        session.sendMessage(message);
    }
    //https://gitee.com/han-dongyu6059/java-web-chat-room-project.git
    @Override
    //在handler类建立正常关闭连接时自动调用
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        System.out.println("handler类连接关闭");
    }

    @Override
    //在handler类建立连接成功后自动调用
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("handler类建立连接成功!");
    }

    @Override
    //在handler类连接异常时自动调用
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        System.out.println("handler连接异常!");
    }
}
